package com.wooophone.common.email;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
import org.apache.log4j.Logger;

/**
 * 邮件发送帮助类
 * 
 * @author MingliC
 */
public class EmailSender {

	private static HtmlEmail htmlEmail;
	private static SimpleEmail simpleEmail;
	private static MultiPartEmail multiPartEmail;

	private static SimpleEmail getSimpleEmailInstance() throws EmailException {
		if (simpleEmail == null) {
			simpleEmail = new SimpleEmail();
			simpleEmail.setTLS(true);
			simpleEmail.setHostName(EmailConfig.getValue("hostName"));
			System.out.println(EmailConfig.getValue("from"));
			simpleEmail.setFrom(EmailConfig.getValue("from"));// 我方
			simpleEmail.setCharset(EmailConfig.getValue("charset"));// 设置Charset
			simpleEmail.setAuthentication(EmailConfig.getValue("username"),
					EmailConfig.getValue("password"));
		}
		return simpleEmail;
	}

	private static MultiPartEmail getMultiEmailInstance() throws EmailException {
		if (multiPartEmail == null) {
			multiPartEmail = new HtmlEmail();
			multiPartEmail.setTLS(true);
			multiPartEmail.setHostName(EmailConfig.getValue("hostName"));
			multiPartEmail.setFrom(EmailConfig.getValue("from"));// 我方
			multiPartEmail.setCharset(EmailConfig.getValue("charset"));// 设置Charset
			multiPartEmail.setAuthentication(EmailConfig.getValue("username"),
					EmailConfig.getValue("password"));
		}
		return multiPartEmail;
	}

	private static HtmlEmail getHtmlEmailInstance() throws EmailException {
		if (htmlEmail == null) {
			htmlEmail = new HtmlEmail();
			htmlEmail.setTLS(true);
			htmlEmail.setHostName(EmailConfig.getValue("hostName"));
			htmlEmail.setFrom(EmailConfig.getValue("from"));// 我方
			htmlEmail.setCharset(EmailConfig.getValue("charset"));// 设置Charset
			htmlEmail.setAuthentication(EmailConfig.getValue("username"),
					EmailConfig.getValue("password"));
		}
		return htmlEmail;
	}

	public static void sendHtmlMail(String content, String subject,
			String sendTo) throws EmailException {
			getHtmlEmailInstance().addTo(sendTo);
			getHtmlEmailInstance().setSubject(subject);// 标题
			getHtmlEmailInstance().setMsg(content);// 内容
			getHtmlEmailInstance().send();
	}

	public static void sendSimpleMail(String content, String subject,
			String sendTo) throws EmailException {
			getSimpleEmailInstance().addTo(sendTo);
			getSimpleEmailInstance().setSubject(subject);// 标题
			getSimpleEmailInstance().setMsg(content);// 内容
			getSimpleEmailInstance().send();
	}

	public static void sendMultiMail(String content, String subject,
			String sendTo) throws EmailException {
			getMultiEmailInstance().addTo(sendTo);
			getMultiEmailInstance().setSubject(subject);// 标题
			getMultiEmailInstance().setMsg(content);// 内容
			getMultiEmailInstance().send();
	}
}
